﻿(function ($) {
    $.widget("lordjs.catalogWidget", {
        // default options
        options: {
            jsonData: null

            // callbacks
        },

        // the constructor
        _create: function () {
            var that = this;
            $.getJSON(this.options.jsonData, function (data) {
                var category_id = "";
                var catalogHtml = "";
                var line = 0;
                for (var i = 0; i < data.length; i++) {
                    line++;
                    category_id = 'category' + (i + 1).toString();

                    /*
                    if (i % 2 == 0) {
                        line++;
                        that.element.append("<div class='box' id='box" + line.toString() + "' class='box'></div>");
                    }
                    */

                    that.element.append("<div class='box' id='box" + line.toString() + "' class='box'></div>");

                    $('#box' + line.toString()).append("<div id='" + category_id + "'></div>");

                    if (i > 1 && i % 2 == 1) {
                        that.element.append("<div style='clear: both;'></div>");
                    }
                    
                    $('#' + category_id).categoryWidget({
                        categoryId: '#' + category_id,
                        category: data[i]
                    });
                }

                $(".box").shapeshift();
            });
        },

        // destructor
        destroy: function () {
            $.Widget.prototype.destroy.call(this);
        }
    });
})(jQuery)
